package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.controller.es.EsUnService;
import com.ruoyi.system.controller.es.dto.EsSearch;
import com.ruoyi.system.domain.vo.DeleteIdVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FyInfo;
import com.ruoyi.system.service.IFyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯Controller
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/system/info")
public class FyInfoController extends BaseController
{
    @Autowired
    private IFyInfoService fyInfoService;
    @Autowired
    private EsUnService esUnService;
    /**
     * 查询资讯列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(FyInfo fyInfo)
    {
        startPage();
        List<FyInfo> list = fyInfoService.selectFyInfoList(fyInfo);
        return getDataTable(list);
    }

    /**
     * 导出资讯列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FyInfo fyInfo)
    {
        List<FyInfo> list = fyInfoService.selectFyInfoList(fyInfo);
        ExcelUtil<FyInfo> util = new ExcelUtil<FyInfo>(FyInfo.class);
        util.exportExcel(response, list, "资讯数据");
    }

    /**
     * 获取资讯详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping( "/getInfo")
    public AjaxResult getInfo(Long infoId)
    {
        return success(fyInfoService.selectFyInfoByInfoId(infoId));
    }

    /**
     * 新增资讯
     */
//    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "资讯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FyInfo fyInfo)
    {
        fyInfo.setInfoId(Long.valueOf(DateUtils.dateTimeNow()));
        EsSearch esSearch=new EsSearch();
        esSearch.setId(fyInfo.getInfoId());
        esSearch.setLanmu("资讯");
        esSearch.setLanmuId(5);
        esSearch.setTitle(fyInfo.getInfoTitle());
        esSearch.setContent(fyInfo.getContent());
        esUnService.save(esSearch);
        return toAjax(fyInfoService.insertFyInfo(fyInfo));
    }

    /**
     * 修改资讯
     */
//    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody FyInfo fyInfo)
    {
        EsSearch esSearch=new EsSearch();
        esSearch.setId(fyInfo.getInfoId());
        esSearch.setLanmu("资讯");
        esSearch.setLanmuId(5);
        esUnService.delete(esSearch);
        esSearch.setTitle(fyInfo.getInfoTitle());
        esSearch.setContent(fyInfo.getContent());
        esUnService.save(esSearch);
        return toAjax(fyInfoService.updateFyInfo(fyInfo));
    }

    /**
     * 删除资讯
     */
//    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "资讯", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DeleteIdVo deleteIdVo)
    {
        return toAjax(fyInfoService.deleteFyInfoByInfoIds(deleteIdVo.getInfoIds()));
    }
}
